var Log = require('../framework/log');
var Messages = require('../core/messages');

function RequestBattleHandler(session,msg){
    var player = session.getPlayer();
    var enemyId = msg.enemyId;
    if(enemyId === player.id){
        Log.debug("RequestBattleHandler Fail,enemyId === player,player = %s",player.id);
        return {ret:false};
    }
    var worldChannel = app.getChannelService().getChannel("world");
    if(worldChannel && worldChannel.hasPlayer(enemyId)){
        var enemyPlayer = worldChannel.getPlayer(enemyId);
        player.setRequestBattlePlayerId(player.id);
        enemyPlayer.getSession().notify(new Messages.BattleNotify(player.id,player.getName()));
        Log.debug("RequestBattleHandler OK,player = %s",player.id);
        return {ret:true};
    }else{
        Log.debug("RequestBattleHandler Fail,enemyId = %s,player = %s",enemyId,player.id);
        return {ret:false};
    }
}

module.exports = RequestBattleHandler;